using System.Data;

namespace homework6
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            textBox1.AutoSize = false;
            textBox1.Height = 68;
        }
        
        static string s = "";
        private void button2_Click(object sender, EventArgs e)
        {
            s += button2.Text;
            textBox1.Text = s;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            s += button1.Text;
            textBox1.Text = s;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            s += button5.Text;
            textBox1.Text = s;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            s += button3.Text;
            textBox1.Text = s;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            s += button4.Text;
            textBox1.Text = s;
        }

        private void button6_Click(object sender, EventArgs e)
        {
            s += button6.Text;
            textBox1.Text = s;
        }

        private void button7_Click(object sender, EventArgs e)
        {
            s += button7.Text;
            textBox1.Text = s;
        }

        private void button8_Click(object sender, EventArgs e)
        {
            s += button8.Text;
            textBox1.Text = s;
        }

        private void button9_Click(object sender, EventArgs e)
        {
            s += button9.Text;
            textBox1.Text = s;
        }

        private void button10_Click(object sender, EventArgs e)
        {
            s += button10.Text;
            textBox1.Text = s;
        }

        private void button11_Click(object sender, EventArgs e)
        {
            if (s.Substring(s.Length - 1) != "+"&& s.Substring(s.Length - 1) !="-"&&
                s.Substring(s.Length - 1) !="*"&& s.Substring(s.Length - 1) !="/" &&
                s.Substring(s.Length - 1) != "."&&s.Substring(s.Length - 1) != "(" && s !=null)
            {
                s += button11.Text;
                textBox1.Text = s;
            }
        }

        private void button12_Click(object sender, EventArgs e)
        {
            if (s.Substring(s.Length - 1) != "+" && s.Substring(s.Length - 1) != "-" &&
                s.Substring(s.Length - 1) != "*" && s.Substring(s.Length - 1) != "/" &&
                s.Substring(s.Length - 1) != "." && s.Substring(s.Length - 1) != "(")
            {
                s += button12.Text;
                textBox1.Text = s;
            }
        }

        private void button13_Click(object sender, EventArgs e)
        {
            if (s.Substring(s.Length - 1) != "+" && s.Substring(s.Length - 1) != "-" &&
                s.Substring(s.Length - 1) != "*" && s.Substring(s.Length - 1) != "/" &&
                s.Substring(s.Length - 1) != "." && s.Substring(s.Length - 1) != "(")
            {
                s += button13.Text;
                textBox1.Text = s;
            }
        }

        private void button14_Click(object sender, EventArgs e)
        {
            if (s.Substring(s.Length - 1) != "+" && s.Substring(s.Length - 1) != "-" &&
                s.Substring(s.Length - 1) != "*" && s.Substring(s.Length - 1) != "/" &&
                s.Substring(s.Length - 1) != "." && s.Substring(s.Length - 1) != "(")
            {
                s += button14.Text;
                textBox1.Text = s;
            }
        }

        private void button15_Click(object sender, EventArgs e)
        {
            if (s.Substring(s.Length - 1) != "+" && s.Substring(s.Length - 1) != "-" &&
                s.Substring(s.Length - 1) != "*" && s.Substring(s.Length - 1) != "/" &&
                s.Substring(s.Length - 1) != "." && s.Substring(s.Length - 1) != "("&&s.Substring(s.Length-1)!=")")
            {
                s+=button15.Text;
                textBox1.Text=s;
            }
        }

        private void button16_Click(object sender, EventArgs e)
        {
                s += button16.Text;
                textBox1.Text = s;
        }

        private void button17_Click(object sender, EventArgs e)
        {
                s += button17.Text;
                textBox1.Text = s;
        }

        private void button18_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
            s = "";
        }

        private void button19_Click(object sender, EventArgs e)
        {
            string result = "";
            if (s!= "")
            {
                try
                {
                    DataTable dataTable = new DataTable();
                    result = dataTable.Compute(s, "").ToString();
                    textBox1.Text = s + "=" + result;
                    s = result;
                }
                catch (Exception)
                {
                    result = "ERRO";
                    textBox1.Text = result;
                }
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged_1(object sender, EventArgs e)
        {

        }

        private void button20_Click(object sender, EventArgs e)
        {
            if (s != null)
            {
                s = s.Substring(0, s.Length - 1);
            }
            textBox1.Text = s;
        }
    }
}